import Taro from '@tarojs/taro';
import { useState, useEffect } from 'react';
import { View, Text, Image } from '@tarojs/components';
import { AtAvatar, AtGrid, AtDivider } from 'taro-ui';
import Login from '@/components/Login';
import "taro-ui/dist/style/components/avatar.scss";
import "taro-ui/dist/style/components/grid.scss";
import "taro-ui/dist/style/components/divider.scss";
import "taro-ui/dist/style/components/icon.scss";
import "taro-ui/dist/style/components/modal.scss";
import css from './index.module.less';
import { iShyk, i_dang, nav1, nav2, nav3, nav4, nav5, nav6, nav7, nav8, nav9 } from './image.js';

export default function Application() {
  const [avatarUrl, setAvatarUrl] = useState("");
  const [modalOpen, setModalOpen] = useState(false);
  const [name, setName] = useState("点击登录/注册");
  const data =
    [
      {
        image: nav1,
        value: '三会一课',

      },
      {
        image: nav2,
        value: '主题党日'
      },
      {
        image: nav3,
        value: '我的支部'
      },
      {
        image: nav4,
        value: '校内转组织'
      },
      {
        image: nav5,
        value: '趣味答题'
      },
      {
        image: nav6,
        value: '志愿活动'
      },
      {
        image: nav7,
        value: '在线留言'
      },
      {
        image: nav8,
        value: '投票调查'
      },
      {
        image: nav9,
        value: '文章发布'
      }
    ];

  //出现登录注册弹窗
  const goLoginRegister = () => {
    if (name === "点击登录/注册") setModalOpen(true);
  };
  useEffect(() => {
    Taro.checkSession({
      success: function () {
        //session_key 未过期，并且在本生命周期一直有效
        Taro.getStorage({
          key: 'userInfo',
          success: function (a) {
            setModalOpen(false);
            setName(a.data.nickname);
            setAvatarUrl(a.data.avatarurl);
          },
          fail: function () {
            Taro.login(); //重新登录
            setModalOpen(true);
          }
        });
      },
      fail: function () {
        // session_key 已经失效，需要重新执行登录流程
        Taro.login(); //重新登录
        setModalOpen(true);
      }
    });
  }, []);
  // 三会一课
  const goShyk = () => {
    Taro.navigateTo({ url: './iShykList/index' });
  };
  const goThematicDay = () => {
    Taro.navigateTo({ url: './thematicdaylist/index' });
  };
  const goMyBranch = () => {
    Taro.navigateTo({ url: '../my/branch/index' });
  };
  const goCampustransfer = () => {
    Taro.navigateTo({ url: './campustransfer/index' });
  };
  const goInterestAsw = () => {
    Taro.navigateTo({ url: './interestasw/index' });
  };
  const goVolunteer = () => {
    Taro.navigateTo({ url: './volunteer/index' });
  };
  const goOnlineMsg = () => {
    Taro.navigateTo({ url: './onlinemsg/index' });
  };
  const goPolls = () => {
    Taro.navigateTo({ url: './pollsandsurvey/index' });
  };
  const goPublishArticle = () => {
    Taro.navigateTo({ url: './publicarticle/index' });
  };

  const itemClick = (item) => {
    if (item.value == '三会一课') {
      //----------------------//
      goShyk();
    }
    if (item.value == '主题党日') {
      //----------------------//
      goThematicDay();
    }
    if (item.value == '我的支部') {
      goMyBranch();
    }
    if (item.value == '校内转组织') {
      goCampustransfer();
    }
    if (item.value == '趣味答题') {
      //----------------------//
      goInterestAsw();
    }
    if (item.value == '志愿活动') {
      //----------------------//
      goVolunteer();
    }
    if (item.value == '在线留言') {
      goOnlineMsg();
    }
    if (item.value == '投票调查') {
      //----------------------//
      goPolls();
    }
    if (item.value == '文章发布') {
      goPublishArticle();
    }
  };
  return (
    <View className={css.app}>
      <View className={css.header}>
        <View className={css.content}>
          <AtAvatar circle image={avatarUrl} className={css.image}></AtAvatar>
          <View className={css.info}>
            <Text className={css.name} onClick={goLoginRegister}>{name}</Text>
            <Text className={css.life}>我的生活组织</Text>
            <Login modalOpen={modalOpen} setModalOpen={setModalOpen} setName={setName} setAvatarUrl={setAvatarUrl} setIsLogin={() => { }} />
          </View>
        </View>
        <View className={css.bottom}>
          <View className={css.box} onClick={goShyk}>
            <Image src={iShyk} />
            <Text className={css.text}>三会一课</Text>
          </View>
          <View className={css.box} onClick={goThematicDay}>
            <Image src={i_dang} />
            <Text className={css.text}>主题党日</Text>
          </View>
        </View>
      </View>
      <View >
        <AtDivider content='基层党建' fontColor='black' />
        <AtGrid data={data} hasBorder={false} onClick={itemClick} ></AtGrid>
      </View>

    </View>
  );
}